package tool;

import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.core.io.ClassPathResource;
import org.yaml.snakeyaml.Yaml;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;

public class YamlToProperties {
    public static void main(String[] args) throws FileNotFoundException {
        String filePath = "E:\\workspace-git\\intelligence\\cloud\\intelligence-scheduler\\target\\classes\\scheduler.yml";
        Map<String, Object> resultMap = yamlToProperties(filePath);
        for (Map.Entry<String, Object> entry : resultMap.entrySet()) {
            System.out.println(entry.getKey() + "=" + entry.getValue());
        }
//        Properties properties = properties();
//        Enumeration<String> enumeration= (Enumeration<String>) properties.propertyNames();
//        while (enumeration.hasMoreElements()){
//            String key = enumeration.nextElement();
//            Object value = properties.getProperty(key);
//            System.out.println(key + "=" + value);
//        }
    }

    private static Map<String, Object> yamlToProperties(String filePath) {
        Yaml yaml = new Yaml();
        InputStream inputStream = null;
        try {
            inputStream = new FileInputStream(filePath);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        Map<String, Object> obj = yaml.load(inputStream);
        Map<String, Object> resultMap = new LinkedHashMap<>();
        toProperties(null, obj, resultMap);
        return resultMap;
    }

    private static void toProperties(String startPrefix, Map<String, Object> yamlMap, Map<String, Object> resultMap) {
        for (Map.Entry<String, Object> entry : yamlMap.entrySet()) {
            String key = entry.getKey();
            Object obj = entry.getValue();
            String subPrefix = "";
            if (startPrefix == null) {
                subPrefix = key;
            } else {
                subPrefix = startPrefix + "." + key;
            }
            if (obj instanceof Map) {
                toProperties(subPrefix, (Map<String, Object>) obj, resultMap);
            } else if (obj instanceof List) {
                List list = (List) obj;
                int i = 0;
                for (Object o : list) {
                    if (o instanceof Map) {
                        toProperties(subPrefix + "[" + i + "]", (Map<String, Object>) o, resultMap);
                    } else {
                        resultMap.put(subPrefix + "[" + i + "]", o);
                    }
                    i++;
                }
            } else {
                resultMap.put(subPrefix, obj);
            }
        }
    }

    public static Properties properties() {
        YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
        yaml.setResources(new ClassPathResource("ism.yml"));
        return yaml.getObject();
    }

}
